﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using eStock.Common;
using eStock.Properties;
using eStock.SQL;

namespace eStock.ChildWindows
{
    /// <summary>
    /// Interaktionslogik für WndSettings.xaml
    /// </summary>
    public partial class WndSettings : Window
    {
        public WndSettings()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            txtdbDatabase.Value = Settings.Default.db_database;
            txtdbPort.Value = Settings.Default.db_port;
            txtdbHost.Value = Settings.Default.db_host;
            txtdbUser.Value = Settings.Default.db_user;
            txtdbPass.Password = Crypto.DecryptString(Settings.Default.db_pass);
            chkUpdateOnClose.IsChecked = Settings.Default.updateOnClose;
        }

        private void BtnTestDBSettingsClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtdbDatabase.Value))
            {
                ErrorHandler.ThrowError(new Exception("das Feld Datenbank darf nicht leer sein."),"Fehler beim Verbinden",false);
                return;
            }
            if (!MainWindow.Database.TryConnect(txtdbHost.Value, txtdbDatabase.Value, txtdbUser.Value, txtdbPass.Password, Convert.ToInt32(txtdbPort.Value)))
            {
                ErrorHandler.ThrowError(MainWindow.Database.LastError,"Fehler beim Verbinden",false);
                imgStatus.Source = Functions.GetImageFromResource("eStock","Images/error.png");
            }
            else
            {
                imgStatus.Source = Functions.GetImageFromResource("eStock", "Images/check2.png");
            }
        }

        private void BtnOkClick(object sender, RoutedEventArgs e)
        {
            if (chkLocalDatabase.IsChecked != null) Settings.Default.db_local = (bool)chkLocalDatabase.IsChecked;
            else Settings.Default.db_local = false;
            Settings.Default.db_database = txtdbDatabase.Value;
            Settings.Default.db_host = txtdbHost.Value;
            Settings.Default.db_user = txtdbUser.Value;
            Settings.Default.db_port = txtdbPort.Value;
            Settings.Default.db_pass = Crypto.CryptString(txtdbPass.Password);
            if (chkUpdateOnClose.IsChecked != null) Settings.Default.updateOnClose = chkUpdateOnClose.IsChecked.Value;
            Settings.Default.Save();

            if(!Globals.Instance.DBLoaded)
            {
                ((MainWindow)Application.Current.MainWindow).LoadDatabase();
            }
            Close();
        }

        private void BtnAbbortClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
    public class NegateConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool)
            {
                return !(bool)value;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool)
            {
                return !(bool)value;
            }
            return value;
        }

    }
}
